//
//  MoreViewController.m
//  sentorise
//
//  Created by admin on 2025/8/22.
//

#import "MoreViewController.h"
#import "GroupAddSPViewController.h"
#import "BlutoothGroupModel.h"
#import "GroupSystemTableViewCell.h"
#import "GlobalDefine.h"
#import "GroupInfoViewController.h"

@interface MoreViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UILabel *addLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;


@property (nonatomic, strong) NSMutableArray<BlutoothGroupModel *> *dataSource;

@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [[NSMutableArray alloc] init];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:kBTBluGroupChanged object:nil];

    [self.tableView registerNib:[UINib nibWithNibName:@"GroupSystemTableViewCell" bundle:nil] forCellReuseIdentifier:@"GroupSystemTableViewCell"];

    [self reloadData];
    
    self.titleLabel.text = NSLocalizedString(@"My Battery system", nil);
    self.addLabel.text = NSLocalizedString(@"Add", nil);
}
//
//- (void)check {
//    Byte dataBytes[7];
//    dataBytes[0] = 0x01;
//    dataBytes[1] = 0x03;
//    dataBytes[2] = 0x02;
//    dataBytes[3] = 0x02; // 0x32为1, 0x02为0
//    dataBytes[4] = 0xA7;
//    dataBytes[5] = 0xEC;
//    dataBytes[6] = 0x9E;
//    NSData *reciveData = [NSData dataWithBytes:dataBytes length:7];
//    if(dataBytes[2] == 0x02 && reciveData.length == 7) {
//        NSInteger num = dataBytes[3] * 256 + dataBytes[4];
//        BOOL cdjrSW = (num >> 13) & 1;
//        BOOL restartSW = (num >> 14) & 1;
//        NSLog(@"111");
//    }
//}

- (void)reloadData {
    [self.dataSource removeAllObjects];
    NSString *groupStr = [[NSUserDefaults standardUserDefaults] objectForKey:BatteryGroupBluList] ?: @"";
    if (groupStr.length) {
        NSArray *dataArray = [BlutoothGroupModel jsonCovertToModel:groupStr];
        NSMutableArray *mlist = [NSMutableArray array];
        for (BlutoothGroupModel *model in dataArray) {
            if (model.bluList.count == 0) {
                [mlist addObject:model];
            }else {
                [mlist addObject:model];
            }
        }
        [self.dataSource addObjectsFromArray:mlist];
    }
    [self.tableView reloadData];
}

- (IBAction)actionAdd:(id)sender {
    GroupAddSPViewController *vc = [[GroupAddSPViewController alloc] initWithNibName:@"GroupAddSPViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GroupSystemTableViewCell *cell = (GroupSystemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"GroupSystemTableViewCell"];
    BlutoothGroupModel *model = self.dataSource[indexPath.section];
    cell.iconIV.image = [UIImage imageNamed:[NSString stringWithFormat:@"system_app_type_%@", model.type ?: @"0"]];
    cell.nameLabel.text = model.groupName;
    cell.spLabel.text = [NSString stringWithFormat:@"%@P%@S", model.p, model.s];
    NSString *type = @[@"RV & Van", @"Mower", @"Boat", @"Golf Cart", @"Electric\nwheelchair", @"ATV", @"Off-grid Cabin", @"household", @"Others"][[(model.type ?: @"0") integerValue]];
    cell.typeLabel.text = NSLocalizedString(type, nil);

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 10)];
    view.backgroundColor = [UIColor clearColor];
    
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GroupInfoViewController *vc = [[GroupInfoViewController alloc] initWithNibName:@"GroupInfoViewController" bundle:nil];
    vc.model = self.dataSource[indexPath.section];
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"Confirm delete?", nil) preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler: nil]];
        [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Confirm", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self.dataSource removeObjectAtIndex:indexPath.section];
            NSString *str = [BlutoothGroupModel toJson:self.dataSource];
            [[NSUserDefaults standardUserDefaults] setObject:str forKey:BatteryGroupBluList];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [self reloadData];
        }]];
        [self presentViewController:alertController animated:true completion:nil];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return NSLocalizedString(@"Delete", nil);
}



@end
